package com.yikes.srb.redis;

import cn.hutool.core.text.CharSequenceUtil;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <p>
 * 分布式锁 Redisson 配置
 * </p>
 *
 * @Author: guanrong.yin
 * @Date: 2024/12/1 23:57
 */
@Configuration
public class RedissonConfig {

    /**
     * IP地址
     */
    @Value("${redisson.address}")
    private String address;

    /**
     * 密码
     */
    @Value("${redisson.password}")
    private String password;

    /**
     * 数据库
     */
    @Value("${redisson.database}")
    private Integer database;

    /**
     * 默认最小空闲连接数
     */
    @Value("${redisson.minIdle}")
    private Integer minIdle;

    @Bean
    public RedissonClient redissonClient() {

        Config config = new Config();

        SingleServerConfig singleServerConfig = config.useSingleServer();
        singleServerConfig.setAddress(address);
        singleServerConfig.setDatabase(database);
        singleServerConfig.setConnectionMinimumIdleSize(minIdle);

        if (CharSequenceUtil.isNotBlank(password)) {
            singleServerConfig.setPassword(password);
        }

        return Redisson.create(config);
    }


}
